package com.ngocvo.QuanLyThuChi;

import java.text.ParseException;
import java.util.Calendar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TimePicker;

public class NoActivity extends Activity{
	
	private static final int HOME = Menu.FIRST;
    
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		menu.add(0, HOME, 0,"Home" ).setIcon(android.R.drawable.ic_menu_info_details);
		return true;
	}
	
	public boolean onOptionsItemSelected(android.view.MenuItem item) {
		switch (item.getItemId()) {
		case HOME:
			Intent i = new Intent(NoActivity.this, StartActivity.class);
			startActivity(i);
			break;
		}
		return true;
	}
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vayno_layout);
		
		//Set flow type
		Global.flowType = FlowType.No;
		//Get date, time, controls
		final TextView lbl_MuonNo = (TextView)findViewById(R.id.lbl_Thu);
		final TextView txt_NgayChoVay = (TextView)findViewById(R.id.txt_Ngay);
		final TextView txt_GioChoVay = (TextView)findViewById(R.id.txt_Gio);
		
		final TextView txt_DateDisplay = (TextView)findViewById(R.id.txt_NgayDisplay);
		final TextView txt_TimeDisplay = (TextView)findViewById(R.id.txt_CustClock);
		final TextView txt_FlowBackDate = (TextView)findViewById(R.id.txt_NgayTraDisplay);
		final EditText edit_Name = (EditText)findViewById(R.id.edit_GhiChu);
		final EditText edit_Amount = (EditText)findViewById(R.id.edit_SoTien);
		final Button btn_Save = (Button)findViewById(R.id.btn_Luu);
		final Button btn_SaveCont = (Button)findViewById(R.id.btn_LuuTiep);
		
		//Change the text to appropriate screen
		lbl_MuonNo.setText("Thêm Khoản Vay Nợ");
		txt_NgayChoVay.setText("Ngày Vay:");
		txt_GioChoVay.setText("Giờ Vay:");
		edit_Name.setHint("Vay của bố...");
		
		//get current date
		Global.updateCurrentDate(txt_DateDisplay, txt_TimeDisplay);
		
		Calendar c = Calendar.getInstance();		
		Global.strCreateDate = String.valueOf(c.get(Calendar.YEAR)) + "-" +
								String.valueOf(Global.pad(c.get(Calendar.MONTH)+1)) + "-" +
								String.valueOf(Global.pad(c.get(Calendar.DAY_OF_MONTH))) + " " +
								String.valueOf(Global.pad(c.get(Calendar.HOUR_OF_DAY))) + ":" +
								String.valueOf(Global.pad(c.get(Calendar.MINUTE))) + ":" +
								String.valueOf(Global.pad(c.get(Calendar.SECOND)));
		
		//Set event listener
		txt_DateDisplay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.DATE_DIALOG_ID);				
			}
		});
		
		txt_TimeDisplay.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.TIME_DIALOG_ID);				
			}
		});
		
		txt_FlowBackDate.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				showDialog(Global.RETURN_DATE_DIALOG_ID);					
			}
		});
	
		//Event listener for btn_Save
		btn_Save.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String Amount = edit_Amount.getText().toString();
				String Name = edit_Name.getText().toString();
				String FlowBackDate = txt_FlowBackDate.getText().toString();
				Global.srtStatus = 0;
				//Save cash flow to db
				try {
					Global.SaveCashFlow(NoActivity.this, Global.flowType, edit_Amount, Amount, Name, FlowBackDate);
				} catch (ParseException e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(NoActivity.this);
					builder.setTitle("Error orrcurs!");
					builder.setMessage("The error is: " + e.getMessage());
					builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub								
						}
					});
					builder.show();
				}
				//Get back to main screen
				Intent i = new Intent(NoActivity.this, StartActivity.class);
				startActivity(i);			
			}
		});
		
		//Event listener for btn_SaveCont
		btn_SaveCont.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				String Amount = edit_Amount.getText().toString();
				String Name = edit_Name.getText().toString();
				String FlowBackDate = txt_FlowBackDate.getText().toString();
				Global.srtStatus = 0;
				//Save cash flow to db
				try {
					Global.SaveCashFlow(NoActivity.this, Global.flowType, edit_Amount, Amount, Name, FlowBackDate);
				} catch (ParseException e) {
					AlertDialog.Builder builder = new AlertDialog.Builder(NoActivity.this);
					builder.setTitle("Error orrcurs!");
					builder.setMessage("The error is: " + e.getMessage());
					builder.setPositiveButton("Continue", new DialogInterface.OnClickListener() {
						
						@Override
						public void onClick(DialogInterface arg0, int arg1) {
							// TODO Auto-generated method stub								
						}
					});
					builder.show();
				}
				//Clear form data
				edit_Amount.setText("");
				edit_Name.setText("");
				Global.updateCurrentDate(txt_DateDisplay, txt_TimeDisplay, txt_FlowBackDate);
			}
		});
	
	}
	//Create date and time dialog
	@Override
	protected Dialog onCreateDialog(int id) {
		
		final TextView txt_DateDisplay = (TextView)findViewById(R.id.txt_NgayDisplay);
		final TextView txt_TimeDisplay = (TextView)findViewById(R.id.txt_CustClock);
		final TextView txt_FlowBackDate = (TextView)findViewById(R.id.txt_NgayTraDisplay);
		
		//Create listener when user set date for flow start date
		DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Global.intYear = year;
				Global.intMonth = monthOfYear + 1;
				Global.intDay = dayOfMonth;
				Global.updateDate(txt_DateDisplay);
			}
		};
		//Create listener when user set date for flow end date
		DatePickerDialog.OnDateSetListener mReturnDateSetListener = new DatePickerDialog.OnDateSetListener() {
			@Override
			public void onDateSet(DatePicker view, int year, int monthOfYear,
					int dayOfMonth) {
				Global.intYear = year;
				Global.intMonth = monthOfYear + 1;
				Global.intDay = dayOfMonth;
				Global.updateFlowBackDate(txt_FlowBackDate);
			}
		};
		
		TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
			
			@Override
			public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
				Global.intHour = hourOfDay;
				Global.intMinute = minute;
				Global.updateTime(txt_TimeDisplay);
			}
		};
		switch(id){
			//Create date picker for flow start date
			case Global.DATE_DIALOG_ID:
				return new DatePickerDialog(this, mDateSetListener, Global.intYear, Global.intMonth - 1, Global.intDay);
			//Create date picker for flow end date
			case Global.RETURN_DATE_DIALOG_ID:
				return new DatePickerDialog(this, mReturnDateSetListener, Global.intYear, Global.intMonth - 1, Global.intDay);
			//Create time picker for start date
			case Global.TIME_DIALOG_ID:
				return new TimePickerDialog(this, mTimeSetListener, Global.intHour, Global.intMinute, true);
		}
		return null;
	}
}
